export function generateChartOptions(config) {
  if (!config.data || !config.data.dimensions || !config.data.source ||
    config.data.dimensions.length === 0 || config.data.source.length === 0) {
    return {
      title: {
        text: config.title || '暂无数据',
        left: 'center',
        top: 'center'
      }
    }
  }

  const { type, data, options = {} } = config

  // 基础配置
  const baseOptions = {
    title: {
      text: config.title,
      left: 'center'
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    legend: {
      type: 'scroll',
      orient: 'horizontal',
      bottom: 10,
      left: 'center',
      data: data.dimensions.slice(1)
    }
  }

  // 根据图表类型生成特定配置
  let typeOptions = {}
  const firstDim = data.dimensions[0]
  const valueDims = data.dimensions.slice(1)

  switch (type) {
    case 'bar':
      typeOptions = {
        xAxis: {
          type: 'category',
          data: data.source.map(item => item[firstDim])
        },
        yAxis: {
          type: 'value'
        },
        series: valueDims.map(dim => ({
          name: dim,
          type: 'bar',
          data: data.source.map(item => item[dim])
        }))
      }
      break

    case 'line':
      typeOptions = {
        xAxis: {
          type: 'category',
          data: data.source.map(item => item[firstDim])
        },
        yAxis: {
          type: 'value'
        },
        series: valueDims.map(dim => ({
          name: dim,
          type: 'line',
          data: data.source.map(item => item[dim])
        }))
      }
      break

    case 'pie':
      typeOptions = {
        series: [{
          type: 'pie',
          radius: '50%',
          center: ['50%', '50%'],
          data: data.source.map(item => ({
            name: item[firstDim],
            value: item[valueDims[0]]
          }))
        }]
      }
      break

    default:
      typeOptions = {
        xAxis: {
          type: 'category',
          data: data.source.map(item => item[firstDim])
        },
        yAxis: {
          type: 'value'
        },
        series: valueDims.map(dim => ({
          name: dim,
          type: 'bar',
          data: data.source.map(item => item[dim])
        }))
      }
  }

  return {
    ...baseOptions,
    ...typeOptions,
    ...options
  }
} 